<!--
 * @Author: wuyue
 * @Date: 2023-02-17 17:48:34
 * @LastEditTime: 2023-03-13 17:21:16
 * @LastEditors: wuyue
 * @Descripttion: 
 * @version: 
-->
<template>
  <dialog-form
    ref="dialogForm"
    :visible.sync="dialogVisible"
    :closeButton="false"
    :edit-model="'E'"
    :form="form"
    :header-title="modalData.title"
    :width="1000"
    :isEmit="false"
    :customFooter="true"
    :rules="modalData.rules"
    :form-list="modalData.formList"
    :destroy-on-close="true"
    @confirm="submitForm"
  >
    <template slot="footer">
      <a-button @click="dialogVisible=false"> 取消 </a-button>
      <a-button type="primary" :loading="loading" @click="submitForm"> {{ modalData.confirmBtnText }} </a-button>
    </template>
  </dialog-form>
</template>

<script>
import DialogForm from '@/components/DialogForm' // 弹窗
import moment from 'moment';

export default {
  name:'a'+Date.now(),
  components: { DialogForm },
  props: {
    modalData: {
      type: Object,
      default: () => {
        return {
          formList: [],
          form: {}
        }
      },
    },
  },
  data () {
    return {
      dialogVisible: false,
      loading: false,
      form: {},
    }
  },
  methods: {
    toggleVisible() {
       this.dialogVisible = !this.dialogVisible
       if (this.dialogVisible) {
            this.$nextTick(() => {
              this.setFormInitValue()
              const row = this.modalData.formList?.find(item => item.keyName === 'fileIds')
              if(row) {
                row.fileArrysData = (e) => {
                  this.fileArrysData('fileIds', e)
                }
              }
            })
       }
    },
    // 设置表单初始值
    setFormInitValue() {
      this.modalData.formList?.forEach(item =>{
        if(item.disabled) {
          this.form[item.keyName] = this.modalData.form[item.keyName]
        } else {
           this.form[item.keyName] = null
        }
      })
      if (this.modalData.title === '计量设备检定记录') {
          this.form.id = this.modalData?.form?.id
          const userInfo = this.$userInfo()
          this.form.fillInUserId = userInfo.id
          this.form.fillInUserName = userInfo.name
          this.form.fillInTime = moment().locale('zh-cn').format('YYYY-MM-DD 00:00:00')
      } else {
         this.form.mdProjectId = this.modalData?.form?.id
         this.form.mdOwnId = this.modalData?.form?.mdOwnId
      }
    },
    fileArrysData(name, e) {
      this.$set(this.form, name, [...e])
    },
    submitForm() {
      this.$refs.dialogForm.$refs.ruleForm.validate(valid => {
        if (valid) {
          let param = {
            ...this.$refs.dialogForm.forms,
            fileIds: this.form.fileIds ? this.form.fileIds.map(item => item.commonId): []
          }
          this.loading = true
          this.modalData.api(param).then((res) => {
            this.loading = false
            this.$message.success('操作成功')
            this.dialogVisible = false
            this.$emit('handle-table-refresh')
          }).finally((res) => {
            this.loading = false
          })
        }
      })
    },
  }
}
</script>
